class Commond {
  constructor() {
    this.servDomain = "https://app.diaochan88.com/";
  }
  tips(title) {
    uni.showToast({
      title,
      icon: "none",
    });
  }
  http(path, param = null, method = "GET", header = {}) {
    const that = this;
    let headers = {
      "content-type": "application/json",
    };

    const token = uni.getStorageSync("token");
    const location = uni.getStorageSync("location");
    if (token) headers.token = token;
    return new Promise((resolve, reject) => {
      uni.showLoading({
        title: "loading...",
        mask: true,
      });
      uni.request({
        url: `${that.servDomain}${path}`,
        method: method,
        header: Object.assign(headers, header),
        data: param,
        success: (res) => {
          // console.log('res==', res);
          if (res.data.code === 400) {
            uni.redirectTo({
              url: "/pages/login/index",
            });
            return;
          }

          resolve(res.data);
        },
        fail: (err) => {
          uni.showToast({
            icon: "error",
            title: "网络连接失败",
            mask: true,
            duration: 1500,
          });
          reject(err);
        },
        complete: () => {
          uni.hideLoading();
        },
      });
    });
  }
  formatTime(time, fmtStr = 0) {
    var date = new Date(time * 1000),
      year = date.getFullYear(),
      month = date.getMonth() + 1,
      day = date.getDate(),
      hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
      minute =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
      second =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    month >= 1 && month <= 9 ? (month = "0" + month) : "";
    day >= 0 && day <= 9 ? (day = "0" + day) : "";

    return { year, month, day, hour, minute, second };
  }
  getLocalPos() {
    return new Promise((resolve, reject) => {
      uni.getLocation({
        type: "wgs84",
        success: (res) => {
          const location = { lat: res.latitude, lng: res.longitude };
          resolve(location);
        },
        fail(e) {
          console.log(e);
          reject(e);
        },
      });
    });
  }
}

export default Commond;

export const totime_day = (timespan) => {
  // 把时间戳转换成时间字符串 yyyy-MM-dd
  const date = new Date(timespan);
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, "0");
  const day = date.getDate().toString().padStart(2, "0");

  const formattedTime = `${year}-${month}-${day}`;
  return formattedTime;
};

export const totime = (timespan) => {
  // 把时间戳转换成时间字符串 yyyy-MM-dd HH:mm:ss
  const date = new Date(timespan);
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, "0");
  const day = date.getDate().toString().padStart(2, "0");
  const hours = date.getHours().toString().padStart(2, "0");
  const minutes = date.getMinutes().toString().padStart(2, "0");
  const seconds = date.getSeconds().toString().padStart(2, "0");
  const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  return formattedTime;
};

const setNum = (num) => {
  if (num < 10) {
    return "0" + num;
  } else {
    return num;
  }
};

export const setInitTime = () => {
  let thisTime = new Date();
  const todayDate =
    thisTime.getFullYear() +
    "-" +
    setNum(thisTime.getMonth() + 1) +
    "-" +
    setNum(thisTime.getDate());
  return todayDate;
};

export const getAddressForPos = (lat, lng) => {
  return new Promise((resolve, reject) => {
    const script = document.createElement("script");
    script.src = `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&output=jsonp&key=UP5BZ-4CYLM-NQM6W-665FE-UWNC7-PWF6J&callback=callback`;
    const callbackName = "callback";
    window[callbackName] = function (data) {
      document.body.removeChild(script);
      delete window[callbackName];
      if (data.status === 0) {
        resolve(data.result.address_component);
      } else {
        resolve({});
      }
    };
    document.body.appendChild(script);
  });
};
